package com.shop.bookstore.controller;

import com.shop.bookstore.entity.Book;
import com.shop.bookstore.entity.Borrow;
import com.shop.bookstore.entity.Reader;
import com.shop.bookstore.entity.User;
import com.shop.bookstore.service.BookService;
import com.shop.bookstore.service.BorrowService;
import com.shop.bookstore.service.ReaderService;
import com.shop.bookstore.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//Controller -- 返回是视图； 使用restController，返回字符串对象
@RestController
@RequestMapping("/api")
public class apiController {
    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;
    @Autowired
    BorrowService borrowService;
    @Autowired
    ReaderService readerService;
    @Autowired
    HttpServletRequest request;
    @RequestMapping("/login")
    public Integer loginProcess(HttpServletRequest request, @RequestParam String username, @RequestParam String userpwd){
//        首先看有没有接受到数据
        System.out.println(username);
        System.out.println(userpwd);
//        如果传递过来的用户名和密码都正确，就直接进入系统管理的主页面main.html
        User user= new User();
        user.setUsername(username);
//        从数据库里查询返回的一个用户对象
        User userdb = userService.findOne(user);
        //        如果用户名和密码有错误，然后就去错误提示页面
        if(!userdb.getUsername().isEmpty()){
            if(userdb.getPassword().equals(userpwd)){
                HttpSession session = request.getSession();
                session.setAttribute("username",username);
                return 1;
            }
            else{
                return 0;
            }
        }else{
            return -1;
        }
    }
    // 注册接口
    @RequestMapping("/register")
    public Integer registerProcess(@RequestParam String username, @RequestParam String userpwd, @RequestParam String confirmPassword) {
        // 首先看有没有接受到数据
        System.out.println(username);
        System.out.println(userpwd);
        System.out.println(confirmPassword);
        // 检查两次输入的密码是否一致
        if (!userpwd.equals(confirmPassword)) {
            return -2; // 密码不一致错误代码
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(userpwd);
        // 将新用户保存到数据库
        int result = userService.save(user);

        if (result > 0) {
            return 1; // 注册成功代码
        } else {
            return -1; // 注册失败代码
        }
    }

    /**
     * 删除商品的方法
     *
     * @param id
     * @return
     */
    @RequestMapping("/bookDelete")
    public Integer bookDelete(@RequestParam String id) {
//        System.out.println("接收到的参数id值是:" + id);
//        根据获取的id来组装一个goods实体对象
        Book book = new Book();
        book.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (bookService.delete(book) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    //    处理更新操作
    @RequestMapping("/bookUpdateProcess")
    public Integer bookUpdateProcess(@RequestParam String id, @RequestParam String bookname, @RequestParam String author,
                                     @RequestParam String type, @RequestParam String publishers,
                                     @RequestParam String number,
                                     @RequestParam String pricing) {
//      组装一个book实体对象
        Book book = new Book(Integer.valueOf(id),bookname,author,Integer.valueOf(number),Float.valueOf(pricing),publishers,type);
        if (bookService.update(book) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    @RequestMapping("/borrowDelete")
    public Integer borrowDelete(@RequestParam String id) {
//        System.out.println("接收到的参数id值是:" + id);
//        根据获取的id来组装一个goods实体对象
        Borrow borrow = new Borrow();
        borrow.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (borrowService.delete(borrow) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    @RequestMapping("/readerDelete")
    public Integer readerDelete(@RequestParam String id) {
//        System.out.println("接收到的参数id值是:" + id);
//        根据获取的id来组装一个goods实体对象
        Reader reader = new Reader();
        reader.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (readerService.delete(reader) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    //    处理更新操作
    @RequestMapping("/borrowUpdateProcess")
    public Integer borrowUpdateProcess(@RequestParam String id,@RequestParam String bookname, @RequestParam String borrow_time,
                                       @RequestParam String borrow_state, @RequestParam String back_time) {
//      组装一个book实体对象
        Borrow borrow = new Borrow(Integer.valueOf(id),bookname,borrow_time,borrow_state,back_time);
        if (borrowService.update(borrow) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    //    处理更新操作
    @RequestMapping("/readerUpdateProcess")
    public Integer readerUpdateProcess(@RequestParam String id,@RequestParam String readername, @RequestParam String idCard,
                                       @RequestParam String borrow_books, @RequestParam String timeout_record) {
//      组装一个book实体对象
        Reader reader = new Reader(Integer.valueOf(id),readername,Integer.valueOf(idCard),borrow_books,Integer.valueOf(timeout_record));
        if (readerService. update(reader) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
}
